﻿Imports Microsoft.TeamFoundation
Imports Microsoft.TeamFoundation.Client
Imports Microsoft.TeamFoundation.WorkItemTracking.Client
Imports Microsoft.TeamFoundation.Framework.Client
Imports System.Net
Imports Microsoft.TeamFoundation.VersionControl.Client

Public Class FileWorkItemHistory

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim configurationServerUri As New Uri(ModuleHost.GetServerAddress)

        Dim configurationServer As New TfsConfigurationServer(configurationServerUri, ModuleHost.GetCredentials())
        Dim tpcService As ITeamProjectCollectionService = configurationServer.GetService(Of ITeamProjectCollectionService)()
        Dim collections = tpcService.GetCollections()
        Dim collection As TfsTeamProjectCollection = configurationServer.GetTeamProjectCollection(collections(0).Id)
        Dim vcs As VersionControlServer = collection.GetService(GetType(VersionControlServer))

        Dim item As Item = vcs.GetItem(TextBox1.Text)
        Dim changeID As Integer = IIf(item.DeletionId <> 0, item.ChangesetId - 1, item.ChangesetId)
        Dim version As ChangesetVersionSpec = New ChangesetVersionSpec(changeID)
        Dim versionFrom As ChangesetVersionSpec = New ChangesetVersionSpec(1)

        Dim changeSets As IEnumerable = vcs.QueryHistory(item.ServerItem, version, 0, RecursionType.None, Nothing, versionFrom, LatestVersionSpec.Latest, Integer.MaxValue, True, False, False, True)

        Dim workItems As New List(Of Integer)
        For Each cs As Changeset In changeSets
            For Each wi As WorkItem In cs.WorkItems
                If Not workItems.Contains(wi.Id) Then
                    workItems.Add(wi.Id)
                End If
            Next
        Next

        For Each i As Integer In workItems
            ListBox1.Items.Add(i)
        Next

    End Sub

    Private Sub pbHelp_Click(sender As System.Object, e As System.EventArgs) Handles pbHelp.Click
        Dim msg As String = "File work item history" + ControlChars.CrLf + _
                            "The purpose of this module is to find the list of work items that participated in the construction of a source file." + ControlChars.CrLf + _
                            "" + ControlChars.CrLf + _
                            "Most source files are products of many requirements and many tasks in a project. They may also be changed for the purpose of fixing many bug items. So it is fair to say the final state that of the work item is the product of all these work items." + ControlChars.CrLf + _
                            "It is not possible to see the list of work items that touched a file through the file's history." + ControlChars.CrLf + _
                            "This module goes through a files changeset history and finds links to work items in each changeset so populates a list of work items that constructed that file."
        MessageBox.Show(msg, "File Work Item History", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

    Private Sub btnSelectFile_Click(sender As System.Object, e As System.EventArgs) Handles btnSelectFile.Click

        Dim configurationServerUri As New Uri(ModuleHost.GetServerAddress)

        Dim configurationServer As New TfsConfigurationServer(configurationServerUri, ModuleHost.GetCredentials())
        Dim tpcService As ITeamProjectCollectionService = configurationServer.GetService(Of ITeamProjectCollectionService)()
        Dim collections = tpcService.GetCollections()
        Dim collection As TfsTeamProjectCollection = configurationServer.GetTeamProjectCollection(collections(0).Id)
        Dim vcs As VersionControlServer = collection.GetService(GetType(VersionControlServer))

        Dim filename As String = ""
        If TFSSourceFileSelector.SelectFile(vcs, filename) Then
            TextBox1.Text = filename
        End If
    End Sub

End Class
